package com.xiaoshou.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoshou.pojo.Files;
import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.QuestionFilesRel;
import com.xiaoshou.pojo.Questiontype;

@Transactional
@Service
public class QuestionTypeService extends BaseService {
	/**
	 * 在上传文件夹中创建问题类型的文件夹，上传文件时直接上传至该文件夹中，便于管理
	 */
	public void createFolderOfQuestionType() {

		List<String> fileStringList = new ArrayList<String>();

		// tomcat的位置
		File fileSup = new File(
				"E:\\apache-tomcat-7.0.25\\webapps\\xiaoshou\\files");
		File[] fileList = fileSup.listFiles();
		if (fileList != null) {
			for (File file : fileList) {
				fileStringList
						.add("E:\\apache-tomcat-7.0.25\\webapps\\xiaoshou\\files"
								+ file.getName());
			}
			List<Questiontype> list = getQuestionTypeDao().findAll();
			for (Questiontype questiontype : list) {
				if (!fileStringList.contains(questiontype.getName())) {
					System.out.println("创建中");
					File ff = new File(
							"E:\\apache-tomcat-7.0.25\\webapps\\xiaoshou\\files\\"
									+ questiontype.getName());
					ff.mkdir();
				} else {
					System.out.println("已经存在该文件夹,不用创建文件夹.");
				}
			}
		} else {
			System.out.println("文件夹中没有文件夹！！！");

			List<Questiontype> list = getQuestionTypeDao().findAll();
			for (Questiontype questiontype : list) {
				File ff = new File(
						"E:\\apache-tomcat-7.0.25\\webapps\\xiaoshou\\files"
								+ questiontype.getName());
				ff.mkdir();
			}
		}

	}

	public Questiontype findQuesionTypeById(String questionTypesId) {
		return getQuestionTypeDao().findById(questionTypesId);

	}

	public List<Questiontype> findAllQuestionList() {
		List<Questiontype> list = getQuestionTypeDao().findAll();
		if (list != null) {
			for (Questiontype questiontype : list) {
				List<Files> addFilesIn = new ArrayList<Files>();
				if (questiontype != null) {
					List<Question> questionList = questiontype.getQuestions();
					for (Question question : questionList) {
						if (question != null) {
							List<QuestionFilesRel> questionFileRelList = question.getQuestionFilesRels();
							for (QuestionFilesRel questionFilesRel : questionFileRelList) {
								if (questionFilesRel != null) {
									addFilesIn.add(questionFilesRel.getFiles());
								}else {
									System.out.println("\t\t\tquestionFilesRel为空值!!!!");
								}
							}
						}else {
							System.out.println("\t\tquestion为空值!!!");
						}
					}
					
					questiontype.setFilesList(addFilesIn);
					if(questiontype!=null){
						questiontype.setSize(questiontype.getFilesList().size());
						System.out.println("设置块中"+questiontype.getSize());
					}
				}else {
					System.out.println("\tquestionType为空值!!");
				}
			}
		}else {
			System.out.println("list为空值!");
		}
		return list;
	}
}
